//
//  HXRelatedVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/12.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXRelatedVC.h"
#import "HX_Macro.h"
#import "HXReltedTCell.h"
#import "HXLearmModel.h"

@interface HXRelatedVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *learm;
@property (nonatomic, strong) UIImageView *imageHeader;
@end

@implementation HXRelatedVC
- (NSMutableArray *)learm {
    if (!_learm) {
        _learm = [NSMutableArray array];
    }
    return _learm;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeaderImage];
    [self tableView];
    [self getDishesRelatedWithModelDishes_id:self.dishes_id];
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 170) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        _tableView.tableHeaderView = _imageHeader;
        [self.view addSubview:_tableView];
        [_tableView registerNib:[UINib nibWithNibName:@"HXReltedTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXReltedTCell"];
    }
    return _tableView;
}
- (void)creatHeaderImage {
    self.imageHeader = [[UIImageView alloc] init];
    _imageHeader.HX_height = 200;
    _imageHeader.HX_width = SCREEN_WIDTH;
    _imageHeader.HX_x = 0;
    _imageHeader.HX_y = 0;
}
- (void)getDishesRelatedWithModelDishes_id:(NSString *)dishes_id {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"methodName=DishesCommensense&dishes_id=%@&version=4.40&", dishes_id];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        HXLearmModel *learmModel = [[HXLearmModel alloc] init];
        learmModel.nutrition_analysis = dicData[@"nutrition_analysis"];
        learmModel.production_direction = dicData[@"production_direction"];
        learmModel.image = dicData[@"image"];
        [self.imageHeader sd_setImageWithURL:[NSURL URLWithString:learmModel.image]];
        [self creatHeaderImage];
        [self.learm addObject:learmModel];
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXReltedTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXReltedTCell"];
    HXLearmModel *learm = [self.learm firstObject];
    NSLog(@"%@", learm.nutrition_analysis);
    if (indexPath.row == 0) {
        cell.name.text = @"相关常识";
        cell.detail.text = learm.nutrition_analysis;
    } else {
        cell.name.text = @"制作指导";
        cell.detail.text = learm.production_direction;
    }
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXLearmModel *learm = [self.learm firstObject];
    CGFloat addHeight = [SelfSizeTools selfSizeHeightWithString:learm.nutrition_analysis Width:SCREEN_WIDTH - 20 Font:[UIFont systemFontOfSize:10]];
    CGFloat guideHeight = [SelfSizeTools selfSizeHeightWithString:learm.production_direction Width:SCREEN_WIDTH - 20 Font:[UIFont systemFontOfSize:10]];
    if (indexPath.row == 0) {
        return 70 + addHeight;
    } else {
        return 70 + guideHeight;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
